<template>
  <div>
    <el-table :data="students" style="width: 100%">
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="studentId" label="学号"></el-table-column>
      <el-table-column prop="class" label="班级"></el-table-column>
      <el-table-column prop="gender" label="性别"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="editStudent(scope.row)" size="small">编辑</el-button>
          <el-button @click="deleteStudent(scope.row.id)" size="small" type="danger">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <el-button @click="openAddDialog" type="primary">添加学生</el-button>
  </div>
</template>

<script>
export default {
  name: 'StudentList',
  data() {
    return {
      students: [
        { id: 1, name: '张三', studentId: '20230001', class: '计算机科学与技术1班', gender: '男' },
        { id: 2, name: '李四', studentId: '20230002', class: '软件工程1班', gender: '女' }
      ]
    };
  },
  methods: {
    editStudent(student) {
      this.$prompt('请输入新的姓名', '编辑学生', {
        inputValue: student.name,
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(({ value }) => {
        student.name = value;
        this.$message.success('更新成功');
      });
    },
    deleteStudent(id) {
      this.students = this.students.filter(s => s.id !== id);
      this.$message.success('删除成功');
    },
    openAddDialog() {
      this.$prompt('请输入学生姓名', '添加学生', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(({ value }) => {
        const newStudent = {
          id: Date.now(),
          name: value,
          studentId: '2023' + Math.floor(Math.random() * 10000),
          class: '未知班级',
          gender: '未知'
        };
        this.students.push(newStudent);
        this.$message.success('添加成功');
      });
    }
  }
};
</script>